<!DOCTYPE html>
<!-- saved from url=(0045)https://mybatis.org/mybatis-3/zh/logging.html -->
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="zh" lang="zh"><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta name="author" content="Clinton Begin">
    <meta name="author" content="Nan Lei">
    <meta name="Date-Revision-yyyymmdd" content="20200309">
    <meta http-equiv="Content-Language" content="zh">
    <title>mybatis – MyBatis 3 | 日志</title>
    <link rel="stylesheet" href="./mybatis – MyBatis 3 _ 日志_files/apache-maven-fluido-1.7.min.css">
    <link rel="stylesheet" href="./mybatis – MyBatis 3 _ 日志_files/site.css">
    <link rel="stylesheet" href="./mybatis – MyBatis 3 _ 日志_files/print.css" media="print">
    <script type="text/javascript" src="./mybatis – MyBatis 3 _ 日志_files/apache-maven-fluido-1.7.min.js.下载"></script>
  </head>
  <body class="topBarDisabled">
    <div class="container-fluid">
      <div id="banner">
        <div class="pull-left"><div id="bannerLeft"><h2>mybatis</h2>
</div>
</div>
        <div class="pull-right"><a href="https://mybatis.org/" id="bannerRight" title="MyBatis logo"><img src="./mybatis – MyBatis 3 _ 日志_files/mybatis-logo.png" alt="MyBatis logo"></a></div>
        <div class="clear"><hr></div>
      </div>

      <div id="breadcrumbs">
        <ul class="breadcrumb">
        <li id="publishDate">最近更新: 09 三月 2020<span class="divider">|</span>
</li>
          <li id="projectVersion">版本: 3.5.4</li>
        </ul>
      </div>
      <div class="row-fluid">
        <div id="leftColumn" class="span2">
          <div class="well sidebar-nav">
    <ul class="nav nav-list">
      <li class="nav-header">参考文档</li>
    <li><a href="https://mybatis.org/mybatis-3/zh/index.html" title="简介"><span class="none"></span>简介</a></li>
    <li><a href="https://mybatis.org/mybatis-3/zh/getting-started.html" title="入门"><span class="none"></span>入门</a></li>
    <li><a href="https://mybatis.org/mybatis-3/zh/configuration.html" title="XML 配置"><span class="icon-chevron-right"></span>XML 配置</a></li>
    <li><a href="https://mybatis.org/mybatis-3/zh/sqlmap-xml.html" title="XML 映射文件"><span class="icon-chevron-right"></span>XML 映射文件</a></li>
    <li><a href="https://mybatis.org/mybatis-3/zh/dynamic-sql.html" title="动态 SQL"><span class="none"></span>动态 SQL</a></li>
    <li><a href="https://mybatis.org/mybatis-3/zh/java-api.html" title="Java API"><span class="icon-chevron-right"></span>Java API</a></li>
    <li><a href="https://mybatis.org/mybatis-3/zh/statement-builders.html" title="SQL 语句构建器"><span class="none"></span>SQL 语句构建器</a></li>
    <li class="active"><a href="https://mybatis.org/mybatis-3/zh/logging.html#"><span class="none"></span>日志</a></li>
      <li class="nav-header">项目文档</li>
    <li><a href="https://mybatis.org/mybatis-3/zh/project-info.html" title="项目信息"><span class="icon-chevron-right"></span>项目信息</a></li>
    <li><a href="https://mybatis.org/mybatis-3/zh/project-reports.html" title="项目报表"><span class="icon-chevron-right"></span>项目报表</a></li>
</ul>
          <hr>
          <div id="poweredBy">
            <div class="clear"></div>
            <div class="clear"></div>
            <div class="clear"></div>
            <div class="clear"></div>
<a href="http://maven.apache.org/" title="构建依靠 Maven" class="poweredBy"><img class="builtBy" alt="构建依靠 Maven" src="./mybatis – MyBatis 3 _ 日志_files/maven-feather.png"></a>
            </div>
          </div>
        </div>
        <div id="bodyColumn" class="span10">


  
    <div class="section">
<h2><a name="a.E6.97.A5.E5.BF.97"></a>日志</h2>
      
<p>Mybatis 通过使用内置的日志工厂提供日志功能。内置日志工厂将会把日志工作委托给下面的实现之一：</p>
      
<ul>
        
<li>
          SLF4J
        </li>
        
<li>
          Apache Commons Logging
        </li>
        
<li>
          Log4j 2
        </li>
        
<li>
          Log4j
        </li>
        
<li>
          JDK logging
        </li>
      </ul>
      
<p>MyBatis 内置日志工厂会基于运行时检测信息选择日志委托实现。它会（按上面罗列的顺序）使用第一个查找到的实现。当没有找到这些实现时，将会禁用日志功能。</p>
      
<p>不少应用服务器（如 Tomcat 和 WebShpere）的类路径中已经包含 Commons Logging。注意，在这种配置环境下，MyBatis 会把 Commons Logging 作为日志工具。这就意味着在诸如 WebSphere 的环境中，由于提供了 Commons Logging 的私有实现，你的 Log4J 配置将被忽略。这个时候你就会感觉很郁闷：看起来 MyBatis 将你的 Log4J 配置忽略掉了（其实是因为在这种配置环境下，MyBatis 使用了 Commons Logging 作为日志实现）。如果你的应用部署在一个类路径已经包含 Commons Logging 的环境中，而你又想使用其它日志实现，你可以通过在 MyBatis 配置文件 mybatis-config.xml 里面添加一项 setting 来选择其它日志实现。</p>
      
<div class="source"><pre class="prettyprint"><span class="tag">&lt;configuration&gt;</span><span class="pln">
  </span><span class="tag">&lt;settings&gt;</span><span class="pln">
    ...
    </span><span class="tag">&lt;setting</span><span class="pln"> </span><span class="atn">name</span><span class="pun">=</span><span class="atv">"logImpl"</span><span class="pln"> </span><span class="atn">value</span><span class="pun">=</span><span class="atv">"LOG4J"</span><span class="tag">/&gt;</span><span class="pln">
    ...
  </span><span class="tag">&lt;/settings&gt;</span><span class="pln">
</span><span class="tag">&lt;/configuration&gt;</span><span class="pln">
      </span></pre></div>
      
<p>可选的值有：SLF4J、LOG4J、LOG4J2、JDK_LOGGING、COMMONS_LOGGING、STDOUT_LOGGING、NO_LOGGING，或者是实现了 <tt>org.apache.ibatis.logging.Log</tt> 接口，且构造方法以字符串为参数的类完全限定名。</p>
      
<p>你也可以调用以下任一方法来选择日志实现：</p>
      
<div class="source"><pre class="prettyprint"><span class="pln">org</span><span class="pun">.</span><span class="pln">apache</span><span class="pun">.</span><span class="pln">ibatis</span><span class="pun">.</span><span class="pln">logging</span><span class="pun">.</span><span class="typ">LogFactory</span><span class="pun">.</span><span class="pln">useSlf4jLogging</span><span class="pun">();</span><span class="pln">
org</span><span class="pun">.</span><span class="pln">apache</span><span class="pun">.</span><span class="pln">ibatis</span><span class="pun">.</span><span class="pln">logging</span><span class="pun">.</span><span class="typ">LogFactory</span><span class="pun">.</span><span class="pln">useLog4JLogging</span><span class="pun">();</span><span class="pln">
org</span><span class="pun">.</span><span class="pln">apache</span><span class="pun">.</span><span class="pln">ibatis</span><span class="pun">.</span><span class="pln">logging</span><span class="pun">.</span><span class="typ">LogFactory</span><span class="pun">.</span><span class="pln">useJdkLogging</span><span class="pun">();</span><span class="pln">
org</span><span class="pun">.</span><span class="pln">apache</span><span class="pun">.</span><span class="pln">ibatis</span><span class="pun">.</span><span class="pln">logging</span><span class="pun">.</span><span class="typ">LogFactory</span><span class="pun">.</span><span class="pln">useCommonsLogging</span><span class="pun">();</span><span class="pln">
org</span><span class="pun">.</span><span class="pln">apache</span><span class="pun">.</span><span class="pln">ibatis</span><span class="pun">.</span><span class="pln">logging</span><span class="pun">.</span><span class="typ">LogFactory</span><span class="pun">.</span><span class="pln">useStdOutLogging</span><span class="pun">();</span></pre></div>
      
<p>你应该在调用其它 MyBatis 方法之前调用以上的某个方法。另外，仅当运行时类路径中存在该日志实现时，日志实现的切换才会生效。如果你的环境中并不存在 Log4J，你却试图调用了相应的方法，MyBatis 就会忽略这一切换请求，并将以默认的查找顺序决定使用的日志实现。</p>
      
<p>关于 SLF4J、Apache Commons Logging、Apache Log4J 和 JDK Logging 的 API 介绍不在本文档介绍范围内。不过，下面的例子可以作为一个快速入门。有关这些日志框架的更多信息，可以参考以下链接：</p>
      
<ul>
        
<li>
          <a class="externalLink" href="http://www.slf4j.org/">SLF4J</a>
        </li>
        
<li>
          <a class="externalLink" href="http://commons.apache.org/logging">Apache Commons Logging</a>
        </li>
        
<li>
          <a class="externalLink" href="http://logging.apache.org/log4j/">Apache Log4j 1.x and 2.x</a>
        </li>
        
<li>
          <a class="externalLink" href="http://java.sun.com/j2se/1.4.1/docs/guide/util/logging/">JDK Logging API</a>
        </li>
      </ul>
      
<div class="section">
<h3><a name="a.E6.97.A5.E5.BF.97.E9.85.8D.E7.BD.AE"></a>日志配置</h3>
      
<p>你可以通过在包、映射类的全限定名、命名空间或全限定语句名上开启日志功能，来查看 MyBatis 的日志语句。</p>
      
<p>再次提醒，具体配置步骤取决于日志实现。接下来我们会以 Log4J 作为示范。配置日志功能非常简单：添加一个或多个配置文件（如 log4j.properties），有时还需要添加 jar 包（如 log4j.jar）。下面的例子将使用 Log4J 来配置完整的日志服务。一共两个步骤：</p>

        
<div class="section">
<h4><a name="a.E6.AD.A5.E9.AA.A4_1.EF.BC.9A.E6.B7.BB.E5.8A.A0_Log4J_.E7.9A.84_jar_.E5.8C.85"></a>
          步骤 1：添加 Log4J 的 jar 包
        </h4>
        
<p>由于我们使用的是 Log4J，我们要确保它的 jar 包可以被应用使用。为此，需要将 jar 包添加到应用的类路径中。Log4J 的 jar 包可以在上面的链接中下载。</p>
        
<p>对于 web 应用或企业级应用，你可以将 <tt>log4j.jar</tt> 添加到 <tt>WEB-INF/lib</tt> 目录下；对于独立应用，可以将它添加到 JVM 的 <tt>-classpath</tt> 启动参数中。</p>

        </div>
<div class="section">
<h4><a name="a.E6.AD.A5.E9.AA.A4_2.EF.BC.9A.E9.85.8D.E7.BD.AE_Log4J"></a>
          步骤 2：配置 Log4J
        </h4>
        
<p>配置 Log4J 比较简单。假设你需要记录这个映射器的日志：</p>
        
<div class="source"><pre class="prettyprint"><span class="kwd">package</span><span class="pln"> org</span><span class="pun">.</span><span class="pln">mybatis</span><span class="pun">.</span><span class="pln">example</span><span class="pun">;</span><span class="pln">
</span><span class="kwd">public</span><span class="pln"> </span><span class="kwd">interface</span><span class="pln"> </span><span class="typ">BlogMapper</span><span class="pln"> </span><span class="pun">{</span><span class="pln">
  </span><span class="lit">@Select</span><span class="pun">(</span><span class="str">"SELECT * FROM blog WHERE id = #{id}"</span><span class="pun">)</span><span class="pln">
  </span><span class="typ">Blog</span><span class="pln"> selectBlog</span><span class="pun">(</span><span class="kwd">int</span><span class="pln"> id</span><span class="pun">);</span><span class="pln">
</span><span class="pun">}</span></pre></div>
        
<p>在应用的类路径中创建一个名为 <tt>log4j.properties</tt> 的文件，文件的具体内容如下：</p>
        
<div class="source"><pre class="prettyprint"><span class="com"># 全局日志配置</span><span class="pln">
log4j</span><span class="pun">.</span><span class="pln">rootLogger</span><span class="pun">=</span><span class="pln">ERROR</span><span class="pun">,</span><span class="pln"> stdout
</span><span class="com"># MyBatis 日志配置</span><span class="pln">
log4j</span><span class="pun">.</span><span class="pln">logger</span><span class="pun">.</span><span class="pln">org</span><span class="pun">.</span><span class="pln">mybatis</span><span class="pun">.</span><span class="pln">example</span><span class="pun">.</span><span class="typ">BlogMapper</span><span class="pun">=</span><span class="pln">TRACE
</span><span class="com"># 控制台输出</span><span class="pln">
log4j</span><span class="pun">.</span><span class="pln">appender</span><span class="pun">.</span><span class="pln">stdout</span><span class="pun">=</span><span class="pln">org</span><span class="pun">.</span><span class="pln">apache</span><span class="pun">.</span><span class="pln">log4j</span><span class="pun">.</span><span class="typ">ConsoleAppender</span><span class="pln">
log4j</span><span class="pun">.</span><span class="pln">appender</span><span class="pun">.</span><span class="pln">stdout</span><span class="pun">.</span><span class="pln">layout</span><span class="pun">=</span><span class="pln">org</span><span class="pun">.</span><span class="pln">apache</span><span class="pun">.</span><span class="pln">log4j</span><span class="pun">.</span><span class="typ">PatternLayout</span><span class="pln">
log4j</span><span class="pun">.</span><span class="pln">appender</span><span class="pun">.</span><span class="pln">stdout</span><span class="pun">.</span><span class="pln">layout</span><span class="pun">.</span><span class="typ">ConversionPattern</span><span class="pun">=%</span><span class="lit">5p</span><span class="pln"> </span><span class="pun">[%</span><span class="pln">t</span><span class="pun">]</span><span class="pln"> </span><span class="pun">-</span><span class="pln"> </span><span class="pun">%</span><span class="pln">m</span><span class="pun">%</span><span class="pln">n</span></pre></div>
        
<p>上述配置将使 Log4J 详细打印 <tt>org.mybatis.example.BlogMapper</tt> 的日志，对于应用的其它部分，只打印错误信息。</p>
        
<p>为了实现更细粒度的日志输出，你也可以只打印特定语句的日志。以下配置将只打印语句 <tt>selectBlog</tt> 的日志：</p>

        
<div class="source"><pre class="prettyprint"><span class="pln">log4j</span><span class="pun">.</span><span class="pln">logger</span><span class="pun">.</span><span class="pln">org</span><span class="pun">.</span><span class="pln">mybatis</span><span class="pun">.</span><span class="pln">example</span><span class="pun">.</span><span class="typ">BlogMapper</span><span class="pun">.</span><span class="pln">selectBlog</span><span class="pun">=</span><span class="pln">TRACE</span></pre></div>
        
<p>或者，你也可以打印一组映射器的日志，只需要打开映射器所在的包的日志功能即可：</p>

        
<div class="source"><pre class="prettyprint"><span class="pln">log4j</span><span class="pun">.</span><span class="pln">logger</span><span class="pun">.</span><span class="pln">org</span><span class="pun">.</span><span class="pln">mybatis</span><span class="pun">.</span><span class="pln">example</span><span class="pun">=</span><span class="pln">TRACE</span></pre></div>
        
<p>某些查询可能会返回庞大的结果集。这时，你可能只想查看 SQL 语句，而忽略返回的结果集。为此，SQL 语句将会在 DEBUG 日志级别下记录（JDK 日志则为 FINE）。返回的结果集则会在 TRACE 日志级别下记录（JDK 日志则为 FINER)。因此，只要将日志级别调整为 DEBUG 即可：</p>

        
<div class="source"><pre class="prettyprint"><span class="pln">log4j</span><span class="pun">.</span><span class="pln">logger</span><span class="pun">.</span><span class="pln">org</span><span class="pun">.</span><span class="pln">mybatis</span><span class="pun">.</span><span class="pln">example</span><span class="pun">=</span><span class="pln">DEBUG</span></pre></div>
        
<p>但如果你要为下面的映射器 XML 文件打印日志，又该怎么办呢？</p>

      
<div class="source"><pre class="prettyprint"><span class="pun">&lt;?</span><span class="pln">xml version</span><span class="pun">=</span><span class="str">"1.0"</span><span class="pln"> encoding</span><span class="pun">=</span><span class="str">"UTF-8"</span><span class="pln"> </span><span class="pun">?&gt;</span><span class="pln">
</span><span class="dec">&lt;!DOCTYPE mapper
  PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
  "http://mybatis.org/dtd/mybatis-3-mapper.dtd"&gt;</span><span class="pln">
</span><span class="tag">&lt;mapper</span><span class="pln"> </span><span class="atn">namespace</span><span class="pun">=</span><span class="atv">"org.mybatis.example.BlogMapper"</span><span class="tag">&gt;</span><span class="pln">
  </span><span class="tag">&lt;select</span><span class="pln"> </span><span class="atn">id</span><span class="pun">=</span><span class="atv">"selectBlog"</span><span class="pln"> </span><span class="atn">resultType</span><span class="pun">=</span><span class="atv">"Blog"</span><span class="tag">&gt;</span><span class="pln">
    select * from Blog where id = #{id}
  </span><span class="tag">&lt;/select&gt;</span><span class="pln">
</span><span class="tag">&lt;/mapper&gt;</span></pre></div>
        
<p>这时，你可以通过打开命名空间的日志功能来对整个 XML 记录日志：</p>

        
<div class="source"><pre class="prettyprint"><span class="pln">log4j</span><span class="pun">.</span><span class="pln">logger</span><span class="pun">.</span><span class="pln">org</span><span class="pun">.</span><span class="pln">mybatis</span><span class="pun">.</span><span class="pln">example</span><span class="pun">.</span><span class="typ">BlogMapper</span><span class="pun">=</span><span class="pln">TRACE</span></pre></div>
        
<p>而要记录具体语句的日志，可以这样做：</p>

        
<div class="source"><pre class="prettyprint"><span class="pln">log4j</span><span class="pun">.</span><span class="pln">logger</span><span class="pun">.</span><span class="pln">org</span><span class="pun">.</span><span class="pln">mybatis</span><span class="pun">.</span><span class="pln">example</span><span class="pun">.</span><span class="typ">BlogMapper</span><span class="pun">.</span><span class="pln">selectBlog</span><span class="pun">=</span><span class="pln">TRACE</span></pre></div>
        
<p>你应该会发现，为映射器和 XML 文件打开日志功能的语句毫无差别。</p>
        
<p><span class="label important">提示</span> 如果你使用的是 SLF4J 或 Log4j 2，MyBatis 会设置 tag 为 MYBATIS。</p>
        
<p>配置文件 <tt>log4j.properties</tt> 的余下内容用来配置输出器（appender），这一内容已经超出本文档的范围。关于 Log4J 的更多内容，可以参考上面的 Log4J 网站。或者，你也可以简单地做个实验，看看不同的配置会产生怎样的效果。</p>
      </div></div>
    </div>
  

        </div>
      </div>
    </div>
    <hr>
    <footer>
      <div class="container-fluid">
        <div class="row-fluid">
            <p>Copyright ©2009–2020
<a href="http://www.mybatis.org/">MyBatis.org</a>.
All rights reserved.</p>
        </div>
      </div>
    </footer>
  

<div class="xl-chrome-ext-bar" id="xl_chrome_ext_{4DB361DE-01F7-4376-B494-639E489D19ED}" style="display: none;">
      <div class="xl-chrome-ext-bar__logo"></div>

      <a id="xl_chrome_ext_download" href="javascript:;" class="xl-chrome-ext-bar__option">下载视频</a>
      <a id="xl_chrome_ext_close" href="javascript:;" class="xl-chrome-ext-bar__close"></a>
    </div></body></html>